<?php

require_once '../MMysql.php';

/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 16/8/18
 * Time: 上午9:17
 */


class search_property
{
    //加载更多
    public function selectProperty($property_id,$type,$low,$high,$city_id)
    {
        $mysql = new MMysql();
        $res='';

        $where=' del_flag=0';

        if($type!=-1){
            $where= $where.'  and property_category='.$type;
        }
        if($low!=-1){
            $where= $where.'  and property_rmb >='.$low;
        }
        if($high!=-1){
            $where= $where.'  and property_rmb <='.$high;
        }
        if($city_id!=-1){
            $where= $where.'  and property_city_id='.$city_id;
        }
        if($property_id>0){
            $where= $where.'  and property_id<'.$property_id;
        }

        $sql="select * from property where $where order by property_id desc limit 6 ;";
        $res=$mysql->doSql($sql);

        $arraya=Array();
        foreach ($res as $u){
            //代理商
            $agentname=$mysql->where(array('agent_id'=> $u["property_agent_id"]))->select('agent');
            $u["agent"]=$agentname[0]['agent_name'];

            $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";

            preg_match_all($pattern,$u['property_imgs'],$match);

            if(count($match[0])>0){
                $u["property_img"]=$match[1][0];
            }
            else{
                $u["property_img"]="";
            }
            $arraya[]=$u;
        }
        echo json_encode($arraya);
    }


    //城市搜索
    public function selectPropertybysearch($type,$low,$high,$city_id)
    {
        $mysql = new MMysql();
        $res='';

        $where=' del_flag=0  and property_city_id='.$city_id ;

        if($type!=-1){
            $where= $where.'  and property_category='.$type;
        }
        if($low!=-1){
            $where= $where.'  and property_rmb >='.$low;
        }
        if($high!=-1){
            $where= $where.'  and property_rmb <='.$high;
        }

        $sql="select * from property where $where order by property_id desc limit 6 ;";

        $res=$mysql->doSql($sql);
        $arraya=Array();
        foreach ($res as $u){
            //代理商
            $agentname=$mysql->where(array('agent_id'=> $u["property_agent_id"]))->select('agent');
            $u["agent"]=$agentname[0]['agent_name'];

            $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
            preg_match_all($pattern,$u['property_imgs'],$match);

            if(count($match[0])>0){
                $u["property_img"]=$match[1][0];
            }
            else{
                $u["property_img"]="";
            }
            $arraya[]=$u;
        }
        echo json_encode($arraya);
    }


    //城市-价格-类型搜索
    public function selectPropertybycityOrtypeOrprice($type,$low,$high,$city_id)
    {
        $mysql = new MMysql();

        $where=' del_flag=0';

        if($type!=-1){
            $where= $where.'  and property_category='.$type;
        }
        if($low!=-1){
            $where= $where.'  and property_rmb >='.$low;
        }
        if($high!=-1){
            $where= $where.'  and property_rmb <='.$high;
        }
        if($city_id!=-1){
            $where= $where.'  and property_city_id='.$city_id;
        }

        $sql="select * from property where $where order by property_id desc limit 6 ;";

        $res=$mysql->doSql($sql);
        $arraya=Array();
        foreach ($res as $u){
            //代理商
            $agentname=$mysql->where(array('agent_id'=> $u["property_agent_id"]))->select('agent');
            $u["agent"]=$agentname[0]['agent_name'];

            $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
            preg_match_all($pattern,$u['property_imgs'],$match);

            if(count($match[0])>0){
                $u["property_img"]=$match[1][0];
            }
            else{
                $u["property_img"]="";
            }
            $arraya[]=$u;
        }
        echo json_encode($arraya);
    }

}

$method=$_REQUEST["method"];

//加载更多
if($method=="more"){

    $property_id =$_REQUEST["property_id"];
    $type =$_REQUEST["type"];
    $low =$_REQUEST["low"];
    $high =$_REQUEST["high"];
    $city_id =$_REQUEST["city_id"];

    $d=new search_property();
    return $d->selectProperty($property_id,$type,$low,$high,$city_id);
}

else if($method=="search"){

    $type =$_REQUEST["type"];
    $low =$_REQUEST["low"];
    $high =$_REQUEST["high"];
    $city_id =$_REQUEST["city_id"];

    $d=new search_property();
    return $d->selectPropertybysearch($type,$low,$high,$city_id);
}
else if($method=="cityOrtypeOrprice"){
    $type =$_REQUEST["type"];
    $low =$_REQUEST["low"];
    $high =$_REQUEST["high"];
    $city_id =$_REQUEST["city_id"];

    $d=new search_property();
    return $d->selectPropertybycityOrtypeOrprice($type,$low,$high,$city_id);
}
